﻿@model ScrumProject.Models.Task
@{
    ViewBag.Title = "WorkHistory";
    ScrumProject.Models.RealizedWorkRepository tr = new ScrumProject.Models.RealizedWorkRepository();
    
    
}

<script type="text/javascript">
    function WorkDoneItem(id, itemTime, taskId, workRemaining) {
        this.id = id;
        this.itemTime = itemTime;
        this.taskId = taskId;
        this.workRemaining = workRemaining;


    }
    $(document).ready(function () {

        $("#workDoneSave").click(function () {
            var ListComp = new Array();
            var listTime = new Array();
            var listItems = new Array();
            var listRemaining = new Array();
            var neg = 0;
            $("input").each(function () {

                if ($(this).attr("id") == "workDone")
                { if ($(this).val() < 0) { neg = 1; } listTime.push($(this).val()); }
                if ($(this).attr("id") == "workId")
                { listItems.push($(this).val()); }
                if ($(this).attr("id") == "workRemaining")
                { if ($(this).val() < 0) { neg = 1; } listRemaining.push($(this).val()); }

            });
            if (neg != 1) {
                for (var i = 0; i <= listTime.length; i++) {
                    var exists = 0;
                    for (var j = 0; j <= ListComp.length; j++) {

                        if (listItems[i] == listItems[j] && i != j) {
                            exists = 1;
                        }
                        if (listTime[i] == undefined && listItems[i] == undefined) {
                            exists = 1;
                        }
                    }

                    var wr = document.getElementById("workRemaining").value;
                    var wid = document.getElementById("taskId").value;
                    if (exists == 0) {

                        var tmp = new WorkDoneItem;
                        tmp.id = listItems[i];
                        tmp.itemTime = listTime[i];
                        tmp.taskId = wid;
                        tmp.workRemaining = listRemaining[i]
                        ListComp.push(tmp);
                    }
                }

                var things = JSON.stringify({ 'ListComp': ListComp });
                $.ajax({
                    type: 'POST',
                    dataType: 'json',
                    url: '/smrpo6/Task/WorkDoneUpdate',
                    contentType: 'application/json; charset=utf-8',
                    data: things


                });
            }
            else {
                alert("negative values are not alowed")
            }

        });



    });

</script>

@using (Html.BeginForm()) {
    
<h2>WorkHistory</h2>
     @Html.ValidationSummary(true)
     <fieldset>
       
     
        <table class="table table-bordered">
        @foreach (var work in Model.RealizedWorks)
        {
            if (tr.checkDate(work) && work.UserId == ((ScrumProject.Models.CustomPrincipal) User).UserId)
            {
            <tr>
                <td>@Html.LabelFor(w => work.DateOfWorking) </td>
                <td> @work.DateOfWorking</td>

                <td> @Html.LabelFor(w => work.WorkDone)  </td>
                <td>@Html.HiddenFor(w => work.Id, new { id = "workId" }) @Html.TextBoxFor(w => work.WorkDone, new { id = "workDone" })</td>
                <td>
                 @Html.LabelFor(w => work.WorkRemaining) </td><td> @Html.TextBoxFor(w => work.WorkRemaining, new { id = "workRemaining" })
                </td>
                
            </tr>
            }
        }

        <tr>
        <td>@Html.HiddenFor(model => model.Id, new { id = "taskId" })</td>
        
         <td></td>
        </tr>
        </table>
        <p>
              <input type="button"  value="Save changes" class="btn" id="workDoneSave" />
              <input type="submit"  value="Done editing" class="btn"  />
              
        </p>
    </fieldset>     


    
}